package com.anrongtec.zcpt;

import android.app.Application;
import android.content.Context;

import com.anrongtec.zcpt.db.LiteOrmDBUtil;
import com.anrongtec.zcpt.model.User;
import com.anrongtec.zcpt.utils.DeviceFactory;
import com.anrongtec.zcpt.utils.GsonUtil;
import com.anrongtec.zcpt.utils.MyLocation;
import com.anrongtec.zcpt.utils.ToastShow;
import com.anrongtec.zcpt.utils.ToolUtils;
import com.lzy.okhttputils.OkHttpUtils;
import com.orhanobut.hawk.Hawk;
import com.socks.library.KLog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangyingying on 2017/1/16.
 */
public class JWTApp extends Application {
    private static JWTApp instance;
    private static Context mContext;
    private static Map<String, String> map;
    private User currentUser;

    //天气
    public static final boolean DEBUG = false;
    public static final boolean USE_SAMPLE_DATA = true;


    public static JWTApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mContext = getApplicationContext();
        initSomeThirdLib();
    }

    private void initSomeThirdLib() {
        Hawk.init(instance).build();
        KLog.init(BuildConfig.DEBUG, ApiConfig.KLOG_TAG);
        LiteOrmDBUtil.createCascadeDB(this);
        initOkHttp();
    }

    private void initOkHttp() {

        // HttpHeaders headers = new HttpHeaders();
        // headers.put("commonHeaderKey1", "commonHeaderValue1"); // 所有的 header
        // 都
        // headers.put("commonHeaderKey2", "commonHeaderValue2");
        // HttpParams params = new HttpParams();
        // params.put("commonParamsKey1", "commonParamsValue1"); // 所有的 params 都
        // 支持
        // // 中文
        // params.put("commonParamsKey2", "这里支持中文参数");
        OkHttpUtils.init(this);
        // 以下都不是必须的，根据需要自行选择
        OkHttpUtils.getInstance()//
                .debug("OkHttpUtils") // 是否打开调试
                .setConnectTimeout(OkHttpUtils.DEFAULT_MILLISECONDS) // 全局的连接超时时间
                .setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) // 全局的读取超时时间
                .setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS);// 全局的写入超时时间
        // .addCommonHeaders(headers) // 设置全局公共头
        // .addCommonParams(params);//添加公共参数
    }

    public static Context getAppCtx() {
        return instance.getApplicationContext();
    }


    /**
     * 获取map格式公共参数,
     *
     * @return
     */
    public Map<String, String> getBaseInfo() {

        if (map == null) {
            // LogUtil.trace(Log.DEBUG, "application", "baseInfo未初始化");
            initBaseInfo();
        }

        return map;
    }
    /**
     * 初始化公共参数, 每次Login页面登录之前调用. map适用于当前登录的用户,切换用户之后重新初始化
     */
    public void initBaseInfo() {
        map = new HashMap<String, String>();
        // map.put("busCode", getBusCode());
        map.put("appType", null);
        map.put("appVersion", ToolUtils.getAppVersionCode(getApplicationContext()) + "");
        map.put("appOs", ToolUtils.getAppOs());
        map.put("deviceSN", ToolUtils.getDeviceSN(getApplicationContext()));
        map.put("deviceType", "Android");
        map.put("mac", ToolUtils.getMAC(getApplicationContext()));
        map.put("ip", ToolUtils.getIpAddr());
        map.put("longitude", MyLocation.getLongitude());
        map.put("latitude", MyLocation.getLatitude());
        map.put("altitude", MyLocation.getAltitude());
        // TODO 以下三项必须和登录时一致
        map.put("imei", ToolUtils.getIMEI(getApplicationContext()));
        map.put("imsi", ToolUtils.getIMSI(getApplicationContext()));
        map.put("uuid", DeviceFactory.getInstance(this).getDeviceUUID());
    }

    public String getBaseInfoJson() {
        if (map == null) {
            initBaseInfo();
        }
        return GsonUtil.getInstance().toJson(map);
    }

    public User getCurrentUser() {
        if (currentUser == null) {
            ToastShow.shortMessage("连接超时,请重新登录");
            currentUser = new User();
        }
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }
}
